package com.ly.hotel.board.ADA628;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import android.content.Context;
import android.media.AudioManager;
import android.util.Log;

import com.ly.hotel.adaptor.HardwareInterface;
import com.ly.hotel.common.HandNoBinder;

/**
 * @author Jouson 与电视接口有关的工具
 */
public class BoardInterfaceAda628 {
	private static Context context = null;

	/*public static TvAudioManager tvAudioManager = null;
	public static TvChannelManager tvChannelManager = null;
	public static TvCommonManager tvCommonManager = null;*/
	
//	public static FactoryManager fm = TvManager.getInstance().getFactoryManager();
	
	private static HandNoBinder handNoBinder;
	/*初始化电视*/
	public static void init(Context c){
		context = c;
		/*if(tvAudioManager == null){
			tvAudioManager = TvAudioManager.getInstance();
		}
		
		if(tvChannelManager == null){
			tvChannelManager = TvChannelManager.getInstance();
		}
		
		if(tvCommonManager == null){
			tvCommonManager = TvCommonManager.getInstance();
		}*/
	}
	
	
	/* 初始化手牌的串口 */
	public static void initBang() {
		handNoBinder = new HandNoBinder() {
		};
		handNoBinder.bind();
	}

	//
	public static void hardwareInit(){
		initBang();
	}
	
	public static void setVolume(int volume){
		//tvAudioManager.setSpeakerVolume(volume);
		
		AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
		audioManager.setStreamVolume(AudioManager.STREAM_SYSTEM,volume, 0);
	}
	
	public static int getVolume(){
		/*Log.i("TvAudioManager", "getSpeakerVolume(), paras volume = " + tvAudioManager.getSpeakerVolume());
		return tvAudioManager.getSpeakerVolume();*/
		return 0;
	}

	//待机
	public static void standbySystem(){
		new Thread(new Runnable()
		{
			@Override
			public void run() {
				try {
					Thread.sleep(5000);
//					tvCommonManager.rebootSystem("standby");
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}).start();

	}
	
	//重启
	public static void rebootSystem(){
		new Thread(new Runnable()
		{
			@Override
			public void run() {
				try {
					Thread.sleep(5000);
//					tvCommonManager.rebootSystem("reboot");
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}).start();

	}
	//设置电视输入源为空，也就是黑屏
	public static void setInputSourceStorage(){
//		tvCommonManager.setInputSource(EnumInputSource.E_INPUT_SOURCE_STORAGE);
	}
	//设置电视输入源ATV
	public static void setInputSourceATV(){
//		tvCommonManager.setInputSource(EnumInputSource.E_INPUT_SOURCE_ATV);
	}
	public static int getCurrentInputSource(){
		/*EnumInputSource v = tvCommonManager.getCurrentInputSource();
		return Integer.parseInt(v.toString());*/
		return 0;
	}
	
	public static void setAtvChannel(int c){
		if(c>=1)
			c--;
		
//		tvChannelManager.setAtvChannel(c);
	}
	
	public static int getCurrentChannelNumber(){
//		return tvChannelManager.getCurrentChannelNumber();
		return 0 ;
	}
	
	public static String getBoardNameAndVersion(){
		String s = null;
		/*try {
			s = TvManager.getInstance().getSystemBoardName();
		} catch (TvCommonException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		return s;
	}
	//恢复视窗为全屏模式
	public static void setFullScreen(){
		/*try {
			VideoWindowType videoWindowType = new VideoWindowType();
			videoWindowType.height = 0xffff;
			videoWindowType.width = 0xffff;

			videoWindowType.x = 0xffff;

			videoWindowType.y = 0xffff;

			TvManager.getInstance().getPictureManager().selectWindow(EnumScalerWindow.E_MAIN_WINDOW);

			TvManager.getInstance().getPictureManager().setDisplayWindow(videoWindowType);

			TvManager.getInstance().getPictureManager().scaleWindow();

		} catch (TvCommonException e) {
			e.printStackTrace();
		}*/
		
	}
	
	public static void writeBytesToI2C(int arg0, short[] arg1, short[] arg2){
		/*try {
			fm.writeBytesToI2C(arg0, arg1, arg2);
		} catch (TvCommonException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
	}
	
	//获取显示屏背光
	public static int getLcdBrighten(Context context){
//		return getPictureSkin().getBacklight();
//		return TvPictureManager.getInstance().getBacklight();
		return 0;
	}
	//设置显示屏背光
	public static void setLcdBrighten(Context context, int value){
//		getPictureSkin().setBacklight(value);
//		TvPictureManager.getInstance().setBacklight(value);

	}	

	//这个是设置到mstar方案主板的音量最大值，其实就是为了防止遥控器或者电视的音量按键控制音量太大声
	//当然啦，普通安卓主板不需要这个鬼东西了！！！！！！！！
	public static void setMaxVolume(int value) {
		String path = "/data/local/setmaxvol.sql";
		File file = new File(path);
		
		try {
			if(file.exists()){
				file.delete();
			}
			file.createNewFile();
			BufferedWriter out = new BufferedWriter(new FileWriter(path));
			out.write(value + "");
			out.close();
		} catch (IOException e1) {
			e1.printStackTrace();
		}

	}
	
	public static void setCallGpioStatus(boolean enable){
		if(enable){
			HardwareInterface.setGpioDeviceStatus(true,1);// 引脚致1
			
			byte[] bi = {'s','l','e','d',',','o','n',(byte)0x0a,(byte)0x0d};
			try {
				if(HandNoBinder.sp != null){
					HandNoBinder.sp.getSerialPort().getOutputStream().write(bi);
					HandNoBinder.sp.getSerialPort().getOutputStream().flush();
				}else if(HandNoBinder.sp2 != null){
					HandNoBinder.sp2.getSerialPort().getOutputStream().write(bi);
					HandNoBinder.sp2.getSerialPort().getOutputStream().flush();
				}else{
					Log.e("serial", "HandNoBinder.sp == null");
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}else{
			HardwareInterface.setGpioDeviceStatus(false,2);// 引脚致1
			
			byte[] bi = {'s','l','e','d',',','o','f','f',(byte)0x0a,(byte)0x0d};
			try {
				if(HandNoBinder.sp != null){
					HandNoBinder.sp.getSerialPort().getOutputStream().write(bi);
					HandNoBinder.sp.getSerialPort().getOutputStream().flush();
				} else if(HandNoBinder.sp2 != null){
					HandNoBinder.sp2.getSerialPort().getOutputStream().write(bi);
					HandNoBinder.sp2.getSerialPort().getOutputStream().flush();
				}else{
					Log.e("serial", "HandNoBinder.sp == null");
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
	}


	public static void setCardLed(boolean on) {
		// TODO Auto-generated method stub
		if(on){
			byte[] bi = {'k','l','e','d',',','o','n',(byte)0x0a,(byte)0x0d};
			try {
				if(HandNoBinder.sp != null){
					HandNoBinder.sp.getSerialPort().getOutputStream().write(bi);
					HandNoBinder.sp.getSerialPort().getOutputStream().flush();
				}else if(HandNoBinder.sp2 != null){
					HandNoBinder.sp2.getSerialPort().getOutputStream().write(bi);
					HandNoBinder.sp2.getSerialPort().getOutputStream().flush();
				}else{
					Log.e("serial", "HandNoBinder.sp == null");
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}else{
			byte[] bi = {'k','l','e','d',',','o','f','f',(byte)0x0a,(byte)0x0d};
			try {
				if(HandNoBinder.sp != null){
					HandNoBinder.sp.getSerialPort().getOutputStream().write(bi);
					HandNoBinder.sp.getSerialPort().getOutputStream().flush();
				} else if(HandNoBinder.sp2 != null){
					HandNoBinder.sp2.getSerialPort().getOutputStream().write(bi);
					HandNoBinder.sp2.getSerialPort().getOutputStream().flush();
				}else{
					Log.e("serial", "HandNoBinder.sp == null");
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
	}

}
